package servlet;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 */
@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/add.cookie":
                addCookie(request, response);
                break;
            case "/find.cookie":
                findCookie(request, response);
                break;
            case "/modify.cookie":
                modifyCookie(request, response);
                break;
            case "/set.cookie":
                setCookie(request, response);
                break;
            case "/remove.cookie":
                removeCookie(request, response);
                break;
            case "/findByName.cookie":
                findByName(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请重新访问</h1>");
                break;
        }
    }

    public void findByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = CookieUtil.findByName("username", request);
        System.out.println("username:" + username);
    }

    public void addCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Cookie cookie = new Cookie("username", "admin");
//        // 将cookie添加到响应对象中
//        // 在返回响应的同时会将对应的cookie信息返回
//        response.addCookie(cookie);
//        Cookie cookie2 = new Cookie("name", "管理员");
//        response.addCookie(cookie2);

        CookieUtil.addCookie("username", "admin", response);
    }

    public void findCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从请求中获取到携带的cookie数据
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + ":" + cookie.getValue());
        }
    }

    public void modifyCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Cookie没有修改方法,但是可以实现修改功能
        // 当添加cookie时,如果已经存在相同key的数据了,则会覆盖原来的
        Cookie cookie = new Cookie("username", "alice");
        response.addCookie(cookie);
    }

    public void setCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("username", "tom");
        // 设置cookie生存时间,单位:秒
        // 当设置了生存时间之后,会话结束时Cookie仍然存在
        // 只有超出生存时间了,cookie才会删除
        cookie.setMaxAge(20);
        response.addCookie(cookie);
    }

    public void removeCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Cookie cookie = new Cookie("name", "");
//        // 当将生存时间设置为0时,即表示删除
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);
        CookieUtil.removeCookie("username", response);
    }
}
